package com.onpositive.wiki3.db.impl;

import java.io.File;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

import com.onpositive.wiki3.db.catmodel.Categories;
import com.onpositive.wiki3.db.catmodel.Category;
import com.onpositive.wiki3.db.catmodel.matches.classify.RelationConstants;
import com.onpositive.wiki3.db.catmodel.matches.classify.RelationStore;

public class Test5 {

	public static void main(String[] args) {
		final WikiDBImpl wikiDBImpl = new WikiDBImpl("C:/work/content");
		try {
			final Categories oc = new Categories(wikiDBImpl, new File(
					"C:/work/content/n2.cats"), false);
			oc.removeAllContaining(wikiDBImpl, "articles_needing");
			oc.removeAllContaining(wikiDBImpl, "categories_named_");
			RelationStore ss = new RelationStore(wikiDBImpl);
			Categories categories = ss.get(RelationConstants.IS_A);
			boolean f = true;
			int os = categories.size();
			while (f) {
				f = false;
				for (Category m : oc) {

					// Category ll = categories.get(m.id());
					if (Character.isDigit(m.toString().charAt(0))) {
						continue;
					}
					if (true || m.toString().split("_").length == 1) {
						// int deepSize = m.deepSize();
						// if (m.deepSize() > 0 && deepSize < 100 * 1000) {
						int[] parents = m.getParents();
						for (int q : parents) {
							Category category = categories.get(q);
							if (category != null) {
								System.out.println(category + "->" + m);
								ss.recordRelation(RelationConstants.IS_A,
										category, m);

							}
						}
						// }
					}
				}
				f = categories.size() != os;
				os = categories.size();
			}
			ss.save();
		} catch (Exception e) {
			e.printStackTrace();
			// TODO: handle exception
		}
	}

	protected static Category[] doTest(final RelationStore ss,
			Category category, HashMap<Category, Integer> i) {
		Category[] childCategories = category.getChildCategories();

		for (Category z : childCategories) {
			Category[] parentsCategories = z.getParentsCategories();
			HashSet<Category> zz = new HashSet<Category>();
			for (Category m : parentsCategories) {
				Set<Category> all2 = ss.getAll(RelationConstants.IS_A, m);
				zz.addAll(all2);
				// System.out.println(all2);

			}
			for (Category z2 : zz) {
				Integer integer = i.get(z2);
				if (integer == null) {
					integer = 0;
				}

				i.put(z2, integer + 1);
			}
		}
		if (!i.isEmpty()) {
			for (Category m : i.keySet()) {
				if (i.get(m) > childCategories.length / 2) {
					System.out.println(m + ":" + i.get(m));
				}
			}
			System.out.println(i);
		}
		return childCategories;
	}
}
